﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LoA.Shared.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace LoA.CameraEngine.Entities {
	internal sealed class GodCameraProcessor : CameraProcessorBase {
		private Vector3 Height;
		private Vector3 Position;
		private Vector3 Target;

		private Vector3 CameraPosition { get { return this.Height + this.Position; } }
		private Vector3 CameraTarget { get { return this.CameraPosition + this.Target; } }

		public GodCameraProcessor(Game game) {
			this.Height = new Vector3(0, 20f, 0);
			this.Position = new Vector3(1f, 0, 1f);
			this.Target = this.Position - this.Height;

			this.ViewAngle = new Vector3(25f, 0, 0);

			this.UpdateViewMatrix( );
		}

		public Vector3 ViewAngle { get { return this.viewAngle; } set { this.viewAngle = value; this.SetNewAngle( ); } }
		private Vector3 viewAngle;

		private void SetNewAngle( ) {
			var yaw = MathHelper.ToRadians(this.ViewAngle.Y);
			var pitch = MathHelper.ToRadians(this.ViewAngle.X);
			var roll = MathHelper.ToRadians(this.ViewAngle.Z);

			this.Target = Vector3.Transform(-this.Height, Matrix.CreateFromYawPitchRoll(yaw, pitch, roll));
		}

		private void UpdateViewMatrix( ) {
			this.ViewMatrix = Matrix.CreateLookAt(this.CameraPosition, this.CameraTarget, Vector3.Up);
		}

		public override void Update(GameTime gameTime, IInput inputEngine) {
			var k = Keyboard.GetState( );

			if (k.IsKeyDown(Keys.Z))
				this.Position.X -= 0.1f;
			if (k.IsKeyDown(Keys.X))
				this.Position.X += 0.1f;
			if (k.IsKeyDown(Keys.C))
				this.Position.Z -= 0.1f;
			if (k.IsKeyDown(Keys.V))
				this.Position.Z += 0.1f;

			this.UpdateViewMatrix( );
		}
	}
}
